import global, { GameRecord } from "../../global";
import Global from "../../global";
import Storage from "../../util/storage";
import Point_ts from "../battle/point";
import GameSkill from "./gameSkill";
import TouchSkill from "./touchSkill";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Frame extends cc.Component {

    @property(cc.Node)
    Frame_smallDown:cc.Node = null;     //落小子确认弹框

    @property(cc.Node)
    Frame_settleAccounts:cc.Node = null;     //结算弹框
    @property(cc.Label)
    Label_settleAccounts:cc.Label = null;       //结算描述

    @property(cc.Node)
    Frame_skillActive:cc.Node = null;         //技能激活弹框
    @property(cc.Label)
    Label_skillActive:cc.Label = null;          //描述

    @property(cc.Node)
    Frame_admitDefeat:cc.Node = null;         //认输激活弹框

    @property(cc.Node)
    Frame_replay:cc.Node = null;                  //复盘退出弹框
    

    onLoad(){
        Global.frame = this;
        if(global.is_replay){
            this.Frame_replay.active = false; 
        }else{
            this.Frame_smallDown.active = false;
            this.Frame_settleAccounts.active = false;
            this.Frame_skillActive.active = false;
            this.Frame_admitDefeat.active = false; 
        }
    }

    /**
     * 弹出弹框 落小确认
     */
    obj:any;
    fake_small:cc.Node;
    openFrameSmallDown(obj:any,fake_small:cc.Node){
        this.obj = obj;
        this.fake_small = fake_small;
        this.Frame_smallDown.active = true;
        this.Frame_smallDown.scale = 0;
        cc.tween(this.Frame_smallDown).to(.2,{scale:1}).start();
    }
    yesSmallDown(){
        this.fake_small.destroy();
        this.Frame_smallDown.active = false;
        Global.client.send(this.obj);
        Global.mgr.pointOffAll();
        //回合结束
        global.client.send({method:"turn",id:global.id});
        global.UI.Button_turnOver.active = false;
        global.UI.Node_label.active = true;
    }
    noSmallDown(){
        if(cc.isValid(this.fake_small)){
            this.fake_small.destroy();
        }
        this.Frame_smallDown.active = false;
        for(let node of Global.mgr.Layer_map.children){
            node.getComponent(Point_ts).onSmallDown();
        }
    }

    /**
     * 结算弹框，保存对局
     */
    openFrameSettleAccounts(winner:number){
        if(winner==-1){
            this.Label_settleAccounts.string = "平局";
        }else if(winner!=global.id){
            this.Label_settleAccounts.string = "你输了";
        }else{
            this.Label_settleAccounts.string = "你赢了";
            //胜场计数+1
            global.data.num_win++;
            Storage.set("data",global.data);
        }
        this.Frame_settleAccounts.active = true;
        this.Frame_settleAccounts.scale = 0;
        cc.tween(this.Frame_settleAccounts).to(.2,{scale:1}).start();
    }
    yesPreserve(){
        let record:Array<GameRecord> = Storage.get("record",true);
        //如果没有记录，则为第一条
        if(!record){
            record = new Array<GameRecord>();
        }else{
            //如果有，判断长度,最多保存3个对局记录
            if(record.length>=3){
                record.shift();
            }
        }
        record.push(global.mgr.gameRecord);
        Storage.set("record",record);
        cc.director.loadScene("index");
    }
    noPreserve(){
        cc.director.loadScene("index");
    }

    /**
     * 激活技能弹框
     */
    gameSkill:GameSkill = null;
    openFrameSkillActive(gameSkill:GameSkill){
        this.gameSkill = gameSkill;
        //这里判断是否有足够的法力值
        if(gameSkill.skill.cost<=global.mgr.me_player.power){
            this.Label_skillActive.string = `是否消耗${this.gameSkill.skill.cost}点法力值激活该技能`;
            this.Frame_skillActive.active = true;
            this.Frame_skillActive.scale = 0;
            cc.tween(this.Frame_skillActive).to(.2,{scale:1}).start();
        }else{
            this.Frame_skillActive.active = false;
            global.persist.tip(`需要${this.gameSkill.skill.cost}点法力值`);
        }
    }
    yesActive(){
        this.Frame_skillActive.active = false;
        this.gameSkill.Sprite_filter.node.active = false;
        this.gameSkill.skill.is_unlock = true;
        global.music_ts.playEffect("Audio_skillUnlock");
        this.gameSkill.skill.time--;
        global.mgr.me_player.power -= this.gameSkill.skill.cost;
        global.websocket.send(JSON.stringify({method:"unlockSkill",id:global.id}));
        //判断是否已经将全部技能解锁
        global.UI.showPower();  
    }
    noActive(){
        this.Frame_skillActive.active = false;
    }

    /**
     * 认输
     */
    openFrameAdmitDefeat(){
        this.Frame_admitDefeat.active = true;
        this.Frame_admitDefeat.scale = 0;
        cc.tween(this.Frame_admitDefeat).to(.2,{scale:1}).start();
    }
    yesAdmitDefeat(){
        global.client.send({method:"admitDefeat",id:global.id});
        this.Frame_admitDefeat.active = false;
        global.UI.Button_turnOver.active = false;
    }
    noAdmitDefeat(){
        this.Frame_admitDefeat.active = false;
    }

    /**
     * 重新复盘
     */
    openFrameReplay(){
        this.Frame_replay.active = true;
        this.Frame_replay.scale = 0;
        cc.tween(this.Frame_replay).to(.2,{scale:1}).start();
    }
    yesExit(){
        cc.director.loadScene("replay");
    }
    noExit(){
        this.Frame_replay.active = false;
    }
}
